/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 *  \file     Uart_Types.h                                                                              *
 *  \brief    This file contains interface header for UAR MCAL driver, ...                              *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2024/10/29     <td>1.0.0                                                                     *
 * </table>                                                                                             *
 *******************************************************************************************************/

#ifndef UART_TYPES_H
#define UART_TYPES_H

#ifdef __cplusplus
extern "C" {
#endif

/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Std_Types.h"
#include "Uart_Cfg.h"
#if (STD_ON == UART_DMA_SUPPORT)
#include "Dma.h"
#endif /** #if (STD_ON == UART_DMA_SUPPORT) */

/********************************************************************************************************
 *                                 Global Macro definition                                              *
 *******************************************************************************************************/
#define UART_MODULE_ID                      2053
#define UART_INSTANCE_ID                    0
#define UART_VENDOR_ID                      43
#define UART_SW_MAJOR_VERSION               1
#define UART_SW_MINOR_VERSION               0
#define UART_SW_PATCH_VERSION               0


#define UART_E_UNINIT                      ((uint8)0x01U)
#define UART_E_AUOTOBAUD_BUSY              ((uint8)0x02U)
#define UART_E_INVALID_CHANNEL             ((uint8)0x03U)
#define UART_E_INVALID_BAUDRATE            ((uint8)0x04U)
#define UART_E_PARAM_POINTER               ((uint8)0x05U)
#define UART_E_ALREADY_INITIALIZED         ((uint8)0x06U)
#define UART_E_INVALID_PARAM               ((uint8)0x07U)
#define UART_E_DIRECTION_TYPE              ((uint8)0x08U)
#define UART_E_CHANNEL_BUSY                ((uint8)0x09U)
#define UART_E_TIMEOUT                     ((uint8)0x0AU)
#define UART_E_SYNC_TRANSFER_TIMEOUT       ((uint8)0x0BU)
#define UART_E_SYNC_TRANSFER_ERR           ((uint8)0x0CU)
#define UART_E_STOP_REALTIME_RECV_FAILED   ((uint8)0x0DU)
#define UART_E_ILLEGAL_RES_ACCESS          ((uint8)0x0EU)
#define UART_E_DMA_REQUEST_CHAN_ERR        ((uint8)0x0FU)


#define UART_INIT_SID                      ((uint8)0x00U)
#define UART_DEINIT_SID                    ((uint8)0x01U)
#define UART_SET_BAUDRATE_SID              ((uint8)0x02U)
#define UART_GET_BAUDRATE_SID              ((uint8)0x03U)
#define UART_SYNC_SEND_SID                 ((uint8)0x04U)
#define UART_SYNC_RECEIVE_SID              ((uint8)0x05U)
#define UART_ASYNC_SEND_SID                ((uint8)0x06U)
#define UART_ASYNC_RECEIVE_SID             ((uint8)0x07U)
#define UART_START_REAL_TIME_RECEIVE_SID   ((uint8)0x08U)
#define UART_STOP_REAL_TIME_RECEIVE_SID    ((uint8)0x09U)
#define UART_ABORT_SID                     ((uint8)0x0AU)
#define UART_GET_STATUS_SID                ((uint8)0x0BU)
#define UART_GET_VERSION_INFO_SID          ((uint8)0x0CU)
#define UART_IP_CLEAR_TX_FIFO_SID          ((uint8)0x0DU)
#define UART_IP_CLEAR_RX_FIFO_SID          ((uint8)0x0EU)
#define UART_IP_WAIT_TX_IDLE_SID           ((uint8)0x0FU)
#define UART_IP_WAIT_RX_IDLE_SID           ((uint8)0x10U)

#define UART_CACHE_LINE CACHE_SIZE

#define UART_DUMMY_STATE(x)   ((x) = (x))

/********************************************************************************************************
 *                                  Global Types definition                                             *
 *******************************************************************************************************/

/**
 *  \brief This type defines the Uart tranfer mode
 */
typedef enum
{
    /** Uart tranfer mode full duplex */
    UART_MODE_FULL_DUPLEX = 0U,
    /** Uart tranfer mode half duplex */
    UART_MODE_HALF_DUPLEX,
} Uart_TranModeType;


/**
 *  \brief This type defines the Uart driver status
 */
typedef enum
{
    /** Uart module is uninitialized */
    UART_UNINIT = 0U,
    /** Uart module is initialized */
    UART_INIT ,
} Uart_DriverStatusType;

/**
 *  \brief This type defines the Uart data direction
 */
typedef enum
{
    /** Uart data direction send */
    UART_SEND    =  (uint8)0x00U,
    /** Uart data direction receive */
    UART_RECEIVE =  (uint8)0x01U,
} Uart_DataDirectionType;

/**
 *  \brief This type defines the Uart transfer status
 */
typedef enum
{
    /** Generic operation success status */
    UART_STATUS_SUCCESS                    = 0x00U,
    /** Generic operation error status */
    UART_STATUS_ERROR                      = 0x01U,
    /** Generic operation busy status */
    UART_STATUS_BUSY                       = 0x02U,
    /** Generic operation timeout status */
    UART_STATUS_TIMEOUT                    = 0x03U,
    /** TX fifo overflow error */
    UART_STATUS_TX_FOVF                    = 0x04U,
    /** RX fifo overflow error */
    UART_STATUS_RX_FOVF                    = 0x05U,
    /** RX fifo underflow error */
    UART_STATUS_RX_FUDF                    = 0x06U,
    /** transfer was aborted */
    UART_STATUS_ABORTED                    = 0x07U,
    /** Framing error */
    UART_STATUS_FRAMING_ERROR              = 0x08U,
    /** Parity error */
    UART_STATUS_PARITY_ERROR               = 0x09U,
    /** Noise error */
    UART_STATUS_NOISE_ERROR                = 0x0AU,
    /** baudrate error */
    UART_STATUS_BAUDRATE_ERROR             = 0x0BU,
    /** stop realtime receive fail */
    UART_STATUS_STOP_REALTIME_RECV_FAIL    = 0x0CU,
} Uart_StatusType;

/**
 *  \brief This type defines the Uart atuobaud state
 */
typedef enum
{
    /** Uart auto baudrate detect not use */
    UART_AUTOBAUD_STATE_NOT_USE = 0x0U,
    /** Uart auto baudrate detect busy state */
    UART_AUTOBAUD_STATE_BUSY,
    /** Uart auto baudrate detect pass state */
    UART_AUTOBAUD_STATE_PASS,
    /** Uart auto baudrate detect fail state */
    UART_AUTOBAUD_STATE_FAIL,
} Uart_AutoBaudSateType;

/**
 *  \brief This type describes the Uart baudrate
 */
typedef uint32 Uart_BaudrateType;

/**
 *  \brief This type defines the Uart match character number
 *         during auto-baudrate detection.
 */
typedef enum
{
    /** match 1 character during auto-baudrate detection. */
    UART_MATCH_NUM_1 = 0U,
    /** match 2 characters during auto-baudrate detection. */
    UART_MATCH_NUM_2,
    /** match 4 characters during auto-baudrate detection. */
    UART_MATCH_NUM_4,
    /** match 8 charactes during auto-baudrate detection. */
    UART_MATCH_NUM_8,
    /** match 16 characters during auto-baudrate detection. */
    UART_MATCH_NUM_16,
    /** match 32 characters during auto-baudrate detection. */
    UART_MATCH_NUM_32,
    /** match 64 characters during auto-baudrate detection. */
    UART_MATCH_NUM_64,
    /** match 128 characters during auto-baudrate detection. */
    UART_MATCH_NUM_128,
} Uart_MatchNumType;

/**
 *  \brief This type defines the Uart tranfer data bit number
 */
typedef enum
{
    /** Uart data bit 8 */
    UART_DATA_BIT_8BIT = 0U,
    /** Uart data bit 7 */
    UART_DATA_BIT_7BIT = 1U,
    /** Uart data bit 6 */
    UART_DATA_BIT_6BIT = 2U,
    /** Uart data bit 5 */
    UART_DATA_BIT_5BIT = 3U,
    /** Uart data bit 4 */
    UART_DATA_BIT_4BIT = 4U,
    /** Uart data bit 9 */
    UART_DATA_BIT_9BIT = 5U,
    /** Uart data bit 16 */
    UART_DATA_BIT_16BIT = 7U,
} Uart_DataBitType;

/**
 *  \brief This type defines the Uart tranfer stop bit number
 */
typedef enum
{
    /** Uart stop bit 1 */
    UART_STOP_BIT_1BIT = 0U,
    /** Uart stop bit 2 */
    UART_STOP_BIT_2BIT = 2U,
} Uart_StopBitsType;

/**
 *  \brief This type defines the Uart tranfer parity type
 */
typedef enum
{
    /** Uart parity disable */
    UART_PARITY_TYPE_DISABLE = 0U,
    /** Uart parity even */
    UART_PARITY_TYPE_EVEN,
    /** Uart parity odd */
    UART_PARITY_TYPE_ODD,
    /** Uart parity always 1 */
    UART_PARITY_TYPE_ALWAYS_1,
    /** Uart parity always 0 */
    UART_PARITY_TYPE_ALWAYS_0,
} Uart_ParityType;

/**
 *  \brief This type defines the Uart tranfer sample rate
 */
typedef enum
{
    /** Uart sample rate 16X */
    UART_SAMPLERATE_16X = 0U,
    /** Uart sample rate 32X */
    UART_SAMPLERATE_32X,
    /** Uart sample rate 8X */
    UART_SAMPLERATE_8X,
    /** Uart sample rate 4X */
    UART_SAMPLERATE_4X,
} Uart_SampleRateType;

/**
 *  \brief This type defines the Uart event. Using for callback function.
 */
typedef enum
{
    /** Uart send complete event */
    UART_EVENT_SEND_COMPLETE = 0U,
    /** Uart receive complete event */
    UART_EVENT_RECV_COMPLETE,
    /** Uart tx error event */
    UART_EVENT_TX_ERROR,
    /** Uart rx error event */
    UART_EVENT_RX_ERROR,
} Uart_EventType;

/**
 *  \brief typedef for normal callback function
 */
typedef void (*Uart_CallBackType)(uint8 channel, Uart_EventType event);

/**
 *  \brief typedef for real time receive callback function
 */
typedef void (*Uart_RealTimeRxCallBackType)(uint8 channel, uint8 * data, uint8 len);

/**
 *  \brief This type describes the Uart channel state
 */
typedef struct
{
    /** indicates the Uart cuurent baudrate */
    uint32 baudRate;
    /** auto baudrate detect state */
    Uart_AutoBaudSateType autoBaudState;
    /** pointer to the buffer of data being sent */
    const uint8 * txBuff;
    /** pointer to the buffer of data being received */
    uint8 * rxBuff;
    /** length of the data being sent */
    volatile uint32 txSize;
    /** length of the data being received */
    volatile uint32 rxSize;
    /** if the send operation is busy */
    volatile boolean isTxBusy;
    /** if the receive operation is busy */
    volatile boolean isRxBusy;
    /** indicates the transmit status of the last operation */
    volatile Uart_StatusType transmitStatus;
    /** indicates the receive status of the last operation */
    volatile Uart_StatusType receiveStatus;
    /** if the realtime receive is started */
    volatile boolean startRealTimeRecv;
#if (STD_ON == UART_DMA_SUPPORT)
    /** dma channel configuration for tx */
    Dma_ChannelConfigType * dmaTxChannel;
    /** dma channel configuration for rx */
    Dma_ChannelConfigType * dmaRxChannel;
#endif /* #if (STD_ON == UART_DMA_SUPPORT) */
} Uart_StateType;

/**
 *  \brief This type describes the Uart channel configuration
 */
typedef struct
{
    /** uart base address */
    uint32 baseAddr;
    /** Baudrate value */
    uint32 baudRate;
    /** data bit control */
    Uart_DataBitType dataBit;
    /** stopBits control*/
    Uart_StopBitsType stopBits;
    /** parity bit control */
    Uart_ParityType parity;
    /** addr bit control */
    boolean addrBit;
    /** sample rate */
    Uart_SampleRateType sampleRate;
    /** auto baudrate detect */
    boolean autoBaud;
    /* expect received match character number during auto-baud */
    Uart_MatchNumType matchNum;
    /** tx fifo water mark level */
    uint8 txwmlvl;
    /** rx fifo water mark level */
    uint8 rxwmlvl;
#if (STD_ON == UART_DMA_SUPPORT)
    /** async transfer using dma */
    boolean transferUsingDma;
    /** dma Peripheral ID */
    Dma_PeripheralIdType dmaPeripheralId;
#endif /* #if (STD_ON == UART_DMA_SUPPORT) */
    /** full/half duplex mode */
    Uart_TranModeType duplexMode;
    /** data fill on transfer */
    uint8 dataFill;
    /** data fill on transfer */
    uint8 assignCoreId;
    /** User callback function */
    Uart_CallBackType callBack;
    /** realtime rx callback function */
    Uart_RealTimeRxCallBackType realTimeRxCallBack;
} Uart_ChannelConfigType;

/** \brief  Type of the external data structure containing the initialization data for uart module. */
typedef struct
{
    /** Channel configuration */
    const Uart_ChannelConfigType *channelConfigs;
} Uart_ConfigType;

#ifdef __cplusplus
}
#endif

#endif /* UART_TYPES_H */

/* End of file */
